package com.gold.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.gold.common.HttpUtils;
import com.gold.common.JsonUtils;
import com.gold.common.StringUtils;
import com.gold.entity.User;
import com.gold.service.UserService;
import com.gold.wechat.entity.WechatLogin;
import com.gold.wechat.service.WechatLoginService;
import com.gold.wechat.utils.WechatLoginPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 登录相关
 *
 * @author XiaoHao
 * @date 2022-06-01 11:07
 */
@Controller
@RequestMapping("wechat/Login")
public class WechatLoginController {
    @Value("${wechat.url}")
    private String URLPATH;
    @Autowired
    WechatLoginService wechatLoginService;
    @Autowired
    UserService userService;

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping(value = "GetQrCode", method = RequestMethod.POST)
    @ResponseBody
    public String GetQrCode() {
        String url = URLPATH + WechatLoginPath.GetQrCode;
        JSONObject object = new JSONObject();
        object.put("proxyIp", "");
        object.put("proxyUserName", "");
        object.put("proxyPassword", "");
        object.put("deviceId", "");
        object.put("deviceName", "");
        //deviceType = 1 用mac登录 其他值忽略
        object.put("deviceType", "0");
        JSONObject jsonObject = HttpUtils.doPost(url, object.toJSONString());
        return jsonObject.toJSONString();
    }


    /**
     * 检查是否登录
     *
     * @return
     */
    @RequestMapping(value = "CheckLogin", method = RequestMethod.POST)
    @ResponseBody
    public String CheckLogin(@RequestBody JSONObject parm) {
        String url = URLPATH + WechatLoginPath.CheckLogin + parm.get("uuid");
        JSONObject jsonObject = HttpUtils.doPost(url, "");
        //获取请求后的数据
        Object obj = jsonObject.get("Data");
        JSONObject data = null;
        if (obj != null) {
            //获取data数据
            data = (JSONObject) obj;
            String wxId = data.getString("WxId");
            //判断微信是否已经登录
            if (StringUtils.isNotEmpty(wxId)) {
                //拿到wxId查询数据库，如果有，则更新，没有，则新增
                WechatLogin wechatLogin = wechatLoginService.selectOne(wxId);
                if (wechatLogin!=null){
                    wechatLogin.setUuid(data.getString("Uuid"));
                    wechatLogin.setWxId(wxId);
                    wechatLoginService.update(wechatLogin);
                }else {
                    wechatLogin.setAlias(data.getString("Alias"));
                    wechatLogin.setDeviceId(data.getString("DeviceId"));
                    wechatLogin.setDeviceName(data.getString("DeviceName"));
                    wechatLogin.setEmail(data.getString("Email"));
                    wechatLogin.setNickName(data.getString("NickName"));
                    wechatLogin.setUuid(data.getString("Uuid"));
                    wechatLogin.setWxId(wxId);
                    wechatLogin.setHeadUrl(data.getString("HeadUrl"));
                    wechatLogin.setMobile(data.getString("Mobile"));
                    wechatLoginService.insert(wechatLogin);
                    //将当前登录用户，和该微信进行绑定关联
                    //获取当前登录的用户信息
                    UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
                    // details里面存放了当前登录用户的详细信息
                    User user = (User) authenticationToken.getDetails();
                    user.setWechatLogin(wechatLogin);
                    userService.updateWechat(user);
                }
                return JsonUtils.responseJson(true, 200, "登录成功", data).toJSONString();
            }
        }
        return JsonUtils.responseJson(false, 200, "未登录", data).toJSONString();
    }


    /**
     * 退出
     *
     * @return
     */
    @RequestMapping(value = "Logout", method = RequestMethod.POST)
    @ResponseBody
    public String Logout(@RequestBody JSONObject parm) {
        String url = URLPATH + WechatLoginPath.Logout + parm.get("wxId");
        JSONObject jsonObject = HttpUtils.doPost(url, parm.toJSONString());
        return jsonObject.toJSONString();
    }

}
